﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Qpm.Automap.Core.UI
{
    public partial class ProjectPeriodForm : Form
    {
        public ProjectPeriodType ProjectPeriod { get; set; }
        public bool IsNameReadOnly { get; set; }
        public bool UseDuration { get; set; }
        public ProjectPeriodForm()
        {
            InitializeComponent();
            UseDuration = true;
            IsNameReadOnly = false;
        }

        private void ProjectPeriodForm_Load(object sender, EventArgs e)
        {
            if (ProjectPeriod != null)
                setControls();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            setProjectPeriod();
        }

        private void setProjectPeriod()
        {
            if (ProjectPeriod == null)
                ProjectPeriod = new ProjectPeriodType();
            ProjectPeriod.Name = new TextType(txt_Name.Text);
            ProjectPeriod.StartDateTime = dt_start.Value.ToString("s");
            ProjectPeriod.EndDateTime = dt_end.Value.ToString("s");
            if (UseDuration)
            {
                ProjectPeriod.DurationMeasure = new DurationMeasureType();
                ProjectPeriod.DurationMeasure.Value = n_duration.Value;
                ProjectPeriod.DurationMeasure.unitCode =
                    (MeasurementUnitCommonCodeDurationContentType)Enum.Parse(typeof(MeasurementUnitCommonCodeDurationContentType), cb_MeasureType.SelectedItem.ToString());
            }
        }

        private void setControls()
        {
            txt_Name.Text = ProjectPeriod.Name.Value;
            txt_Name.ReadOnly = IsNameReadOnly;
            dt_start.Value = DateTime.Parse(ProjectPeriod.StartDateTime);
            dt_end.Value = DateTime.Parse(ProjectPeriod.EndDateTime);
            if (UseDuration)
            {
                n_duration.Value = ProjectPeriod.DurationMeasure.Value;
                cb_MeasureType.SelectedItem = ProjectPeriod.DurationMeasure.unitCode.ToString();
            }
            else
            {
                n_duration.Visible = false;
                label4.Visible = false;
                label5.Visible = false;
                cb_MeasureType.Visible = false;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ProjectPeriod = null;
            this.Close();
        }
    }

    public class ProjectPeriodFormParams
    {
        public ProjectPeriodFormParams()
        {
            IsNameReadOnly = false;
            UseDuration = true;
            ProjectPeriod = new ProjectPeriodType();
        }
        public ProjectPeriodFormParams(ProjectPeriodType period, bool isNameReadOnly, bool useDuration)
        {
            IsNameReadOnly = isNameReadOnly;
            UseDuration = useDuration;
            ProjectPeriod = period;
        }
        public ProjectPeriodType ProjectPeriod { get; set; }
        public bool IsNameReadOnly { get; set; }
        public bool UseDuration { get; set; }
    }
}
